package com.ant.storage.dataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/12/26 11:34
 */
public class DynamicDataSourceContextHolder {

    public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);

    /**
     * 使用ThreadLocal维护变量
     */
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    /**
     * 设置数据源的变量
     */
    public static void setDataSourceType(String dsType) {
        log.info("切换到{}数据源", dsType);
        contextHolder.set(dsType);
    }

    /**
     * 获得数据源的变量，默认使用主数据源
     */
    public static String getDataSourceType()
    {
       /// return contextHolder.get() == null ? DataSourceType.MASTER.name() : contextHolder.get();
        return contextHolder.get();
    }
    /**
     * 清空数据源变量
     */
    public static void clearDataSource()
    {
        contextHolder.remove(); log.info("已切换到主数据源");
    }
}
